#include <bits/stdc++.h>
using namespace std;
const int N=1e5+50;
typedef long long ll;
int n,m;
ll s,r;
vector<ll> sr[N];
bool cmp(int a,int b){
    return a>b;
}
ll res[N];
int main(void){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%lld%lld",&s,&r);
        sr[s].push_back(r);
    }
    for(int i=1;i<=m;i++){
        int l=sr[i].size();
        if(l==0){
            continue;
        }
        sort(sr[i].begin(),sr[i].end(),cmp);
        ll t=0;
        for(int j=0;j<l;j++){
            t+=sr[i][j];
            if(t>0){
                res[j+1]+=t;
            }else{
                break;
            }
        }
    }
    ll ans=-1;
    for(int i=1;i<=n;i++){
        ans=max(ans,res[i]);
    }
    printf("%lld\n",ans);
    return 0;
}